import { ConversationGroupList, modelList } from "@/api/chat";
import { clerStorage } from "./storage/storage"
import router from "@/router"
import { useChatStoreWithOut } from "@/store/modules/chat";
import { userInfo } from "@/types/app";
import { ConversationGroupListFace, ModelTypeCace } from "@/types/chat.type";
import JSEncrypt from "jsencrypt";
const chatStore = useChatStoreWithOut();
const emp = ['null', 'undefined']
export const logout = () => {
  clerStorage()
  router?.push('/login')
}

export const isEmpData = (k: string) => {
  return emp.includes(k)
}
// 使用RSA密码加密方式
export const encrypt = (text: string) => {
  const encrypt = new JSEncrypt();
  encrypt.setPublicKey(
    `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt4VKlnBJ8ZqtL/975xzG
      0WrUlHjcJJwWifVscuJH6Gv652uVqb8B0c5cvB/sMGPhb6BObdFsiPwfwO3ahDem
      3pglH4Ugp/+auy8Hia8bXum4qXlVWvyXuXTZBXtqV8d5/scw4qXRXn5/PGX5nKHK
      3sDhs3rkE0FWE30C5jZ3EW+b58RRau5gogHyjSXEQybLkohCP0YkMj5wM4QZgSgU
      s1ch219wfl2X3BdMkU9whH4W28by3xJrUAcPcpHOBTF7TRGCmTmzLD9wz8QFnlU7
      7GYvduCA3Hp5bxzdsbmKeCWu5MFQQQ6cTRNRzGjKv5+FzLmv3efQRcO0yXIKRd6K
      kwIDAQAB`,
  );
  const encrypted = encrypt.encrypt(text);
  return encrypted;
};


export function smoothScrollToBottom(id: string, duration = 1000) {
  const element = document.getElementById(id);
  if (!element) return;
  let s = element.scrollTop
  function animation() {
    s += duration
    element.scrollTo(0, s);
    if (element.scrollHeight - s > duration) {
      requestAnimationFrame(animation)
    }
  }
  requestAnimationFrame(animation)
}

export const initChat = async (res: userInfo) => {
  const modelListRes = await modelList<{
    list: Array<ModelTypeCace>;
    total: number;
  }>()
  chatStore.setCurrentModelId(res.modelId)
  chatStore.setCurrentModel(res.modelName)
  chatStore.setModelList(modelListRes.list)
  const data = modelListRes.list.find(e => e.id == res.modelId)
  if (data) {
    const models = data.models.split(',')
    chatStore.setModels(models)
  }
  
}
export const initConversationGroup =  async ()=>{
 const GroupRes = await ConversationGroupList<{
      list: ConversationGroupListFace[];
      total: number;
    }>();
    chatStore.setConversationGroup(GroupRes.list)
}